YACC specification to check syntax of if and if else statements of C language 
//if.l
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]   
if    return IF;
else    return ELSE;
elseif return ELSEIF;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%


//if.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM IF  LE GE EQ NE OR AND ELSE ELSEIF
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%

S      : ST {printf("Input accepted.\n");exit(0);};
ST    : IF '(' E2 ')'  ST1';' ELSE ST1';'
        | IF '(' E2 ')'  ST1';'
        | IF '(' E2 ')'  ST1';' ELSEIF'(' E2 ')' ST1';'
        ;
ST1  : ST
        | E
        ;
E    : ID'='E
      | E'+'E
      | E'-'E
      | E'*'E
      | E'/'E
      | E'<'E
      | E'>'E
      | E LE E
      | E GE E
      | E EQ E
      | E NE E
      | E OR E
      | E AND E
      | ID
      | NUM
      ;
E2  : E'<'E
      | E'>'E
      | E LE E
      | E GE E
      | E EQ E
      | E NE E
      | E OR E
      | E AND E
      | ID
      | NUM
      ;

%%

#include "lex.yy.c"

main()
{
  printf("Enter the exp: ");
  yyparse();
}

Output:

students@cselab-desktop:~$ lex if.l
students@cselab-desktop:~$ yacc if.y
students@cselab-desktop:~$ gcc y.tab.c -ll -ly
students@cselab-desktop:~$ ./a.out
Enter the exp: if(a==1)  b=1; else b=2;
Input accepted.
students@cselab-desktop:~$


